/**
 * --------------------------------------------------------------------------
 * 用 ES6 的限定作用域 Symbol 实现 Stack 类
 * --------------------------------------------------------------------------
 */
const items = Symbol('stackItems')

class Stack {
  constructor() {
    this[items] = []
  }

  push(element) {
    this[items].push(element)
  }
  pop() {
    return this[items].pop()
  }

  peek() {
    return this[items][this[items].length - 1]
  }

  isEmpty() {
    return this[items].length === 0
  }

  size() {
    return this[items].length
  }

  clear() {
    this[items] = []
  }

  print() {
    console.log(this.toString())
  }

  toString() {
    return this[items].toString()
  }
}

const stack = new Stack()
stack.push(5)
stack.push(8)
const objectSymbols = Object.getOwnPropertySymbols(stack)
console.log(objectSymbols.length) // 1
console.log(objectSymbols) // [Symbol()]
console.log(objectSymbols[0]) // Symbol()
stack[objectSymbols[0]].push(1)
stack.print() // 5, 8, 1
